package hz.mobileproject.pda.ui.frgment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.MessageEvent;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.request.Request_getNoneProcess;
import hz.mobileproject.pda.entity.response.Response_noneProcess_item;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.activity.work.GridPersonActivity;
import hz.mobileproject.pda.ui.adapter.NoneProcessAdapter;
import hz.mobileproject.pda.utils.Base64;

/**
 * Created by zmybi on 2017/10/23.
 */

public class NoneProcessFragment extends Fragment implements AdapterView.OnItemClickListener {
    private ListView lv_noneProcess;

    private Context mContext;
    private SPUtils spUtils;
    private Gson mGson;
    private String cus_number;
    private String dept_id;
    private NoneProcessAdapter npAdapter;

    public static NoneProcessFragment getInstance() {
        return new NoneProcessFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_none_process, null);
        lv_noneProcess = (ListView) v.findViewById(R.id.lv_noneProcess);
        lv_noneProcess.setOnItemClickListener(this);
        initData();
        return v;
    }

    private void initData() {
        mContext = getContext();
        mGson = new Gson();
        spUtils = SPUtils.getInstance("app_login");
        cus_number = spUtils.getString("cusnumber");
        dept_id = spUtils.getString("department_id");
        npAdapter = new NoneProcessAdapter(mContext,new ArrayList<Response_noneProcess_item>());
        lv_noneProcess.setAdapter(npAdapter);
        //测试填充数据
//        dept_id = "68";

        EventBus.getDefault().register(this);


    }

    @Override
    public void onResume() {
        super.onResume();
        getNoneProcessList();
    }

    /**
     * 获取列表数据请求
     */
    private void getNoneProcessList(){
        Request_getNoneProcess gnp = new Request_getNoneProcess(cus_number,dept_id);
        String gnpStr = mGson.toJson(gnp);
        LogUtils.e(gnpStr);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(gnpStr));
        String body_data = mGson.toJson(data);

        RequestParams params = new RequestParams(Constance.BASE_URL + "/" + Constance.PROJECT_NAME + "/grid/noProcessingList");
        params.setBodyContent(body_data);
        x.http().post(params, callback);
    }


    /**
     * 获取列表数据回调
     */
    private Callback.CommonCallback<String> callback = new Callback.CommonCallback<String>() {
        @Override
        public void onSuccess(String result) {
            try {
                LogUtils.e("未处理请求网络了");
                JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                String returnStr = Base64.getFromBase64(returnData.get("data").getAsString());
                LogUtils.e(returnStr);
                ArrayList<Response_noneProcess_item> listData = mGson.fromJson(returnStr,
                        new TypeToken<ArrayList<Response_noneProcess_item>>() {}.getType());
                npAdapter.setListData(listData);
                if(listData==null || listData.size()==0){
                    Toast.makeText(mContext,"没有更多数据了",Toast.LENGTH_LONG).show();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            LogUtils.e(ex.toString());
            Toast.makeText(mContext,"获取数据失败",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };

    /**
     * listview item点击事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Response_noneProcess_item np = npAdapter.getListData().get(position);
        Intent it = new Intent(mContext, GridPersonActivity.class);
        it.putExtra("np",  np);
        startActivity(it);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event){
        //收到Activity的消息
        getNoneProcessList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
